package com.way.Java多线程编程核心技术v2.chapter02_对象及变量的并发访问._2_2_synchronized同步语句块.不同步导致的逻辑错误及其解决方法;

/**
 * @PROJECT_NAME: my-projects
 * @DESCRIPTION:
 * @USER: 魏彪
 * @DATE: 2023/11/29 15:30
 */
public class MyService {

    public MyOneList addServiceMethod(MyOneList list, String data) {
        try {
            synchronized (list) {
                if (list.getSize() < 1) {
                    // 模拟从远程端花费2s取回数据
                    Thread.sleep(2000);
                    list.add(data);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
